package com.example.source_share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.source_share.mapper.UserMapper;
import com.example.source_share.pojo.User;
import com.example.source_share.service.impl.utils.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class userDetailServiceImpl implements UserDetailsService {//用于用户认证，继承userDetailService接口
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{//登入
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user= userMapper.selectOne(queryWrapper); //从数据库中选出该用户信息，存入user
        if(user==null)//用户名不存在
            throw new RuntimeException("用户不存在");

        return new UserDetailsImpl(user);
    }
}
